/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.bioteam.appweb.gwt.apps.clientandserver.LoginProxy;
import net.bioteam.appweb.gwt.apps.clientandserver.LoginProxyAsync;
import net.bioteam.common.user.User;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.layout.HLayout;

public class ReloginWindow extends Window
{

	final DynamicForm form;
	final PasswordItem password;

	String username;
	List<Command> cmds = new LinkedList<Command>();
	public Label messageLabel;

	public void addCommand(Command cmd)
	{
		cmds.add(cmd);
	}

	int count = 0;

	public ReloginWindow()
	{

		setWidth(300);
		setHeight(100);
		setTitle("Please reenter password");
		setShowMinimizeButton(false);
		setIsModal(true);
		centerInPage();
		setAutoSize(true);
		setLayoutMargin(30);
		setMembersMargin(10);
		username = LoginHolder.getUsername();

		messageLabel = new Label();
		messageLabel.setStyleName("red");
		messageLabel.setHeight(30);
		messageLabel.setWidth100();
		messageLabel.setAlign(Alignment.CENTER);
		addItem(messageLabel);

		addCloseClickHandler(new CloseClickHandler()
		{

			public void onCloseClick(CloseClientEvent event)
			{
				hide();
				password.setValue("");

			}
		});

		form = new DynamicForm();
		form.setHeight100();
		form.setWidth(300);
		form.setPadding(5);

		password = new PasswordItem("password");
		password.setTitle("Please re-enter the password");
		form.setItems(password);

		form.setLayoutAlign(Alignment.CENTER);

		addItem(form);

//		LayoutSpacer spacer = new LayoutSpacer();
//		spacer.setHeight(20);
//		addItem(spacer);

		HLayout h=new HLayout();
		h.setWidth(100);
		h.setHeight(20);
		h.setBackgroundColor("red");
		h.setLayoutAlign(Alignment.CENTER);
		IButton button = new IButton("ReLogin");
		button.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				Map<String, String> values = new HashMap<String, String>();

				if (password.getValue() == null || password.getValue().toString().length() == 0)
					messageLabel.setContents("password is required");

				// User user=new User();
				values.put("username", username);

				values.put("password", LoginHolder.hashPassword(username, password.getValue().toString()));
				LoginProxyAsync login = LoginProxy.Util.getInstance();
				login.login(values, new AsyncCallback<User>()
				{

					public void onFailure(Throwable caught)
					{
						count++;
						if (count == 3)
							StaticHolder.loginPanel.logout();
						else
						{
							messageLabel.setContents(caught.getMessage());
							password.setValue("");
						}

					}

					public void onSuccess(User result)
					{
						password.setValue("");
						ReloginWindow.this.hide();
						for (Command c : cmds)
						{
							c.execute();
						}
						cmds.clear();

					}

				});
			}
		});
		h.addMember(button);

		
		IButton logout = new IButton("Logout");
		logout.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				StaticHolder.loginPanel.logout();
			}
		});
		h.addMember(logout);
		addItem(h);

	}

}
